//
//  CommentVC.m
//  Echo
//
//  Created by Cospring on 14/12/17.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "CommentVC.h"
#import "CommentCell.h"
#import "UIConfig.h"



@interface CommentVC ()

@end


@implementation CommentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    
    //UITableViewCellSeparatorStyle
    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    [self.commentTableView registerNib:[UINib nibWithNibName:@"CommentCell" bundle:nil] forCellReuseIdentifier:@"CommentCell"];
    
    self.commentDataList = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < 30; i++) {
        [self.commentDataList addObject:@{@"name":@"oo春寒料峭oo", @"text":@"做英语阅读的感觉：我们知道賢鏛是在生活中很重要的。比如在鼙蠻和贎鬍里，有彃燊在罅鷄那里蘩墝，之前他们鏈鴊恆闳嘑傡彚槩滼鞷蕻賤鬡艐倏雫寬褲灣曠隗谡堅鑷蠵。1，鞷 在文中的意思？ 2，这篇文章的最佳标题？3，作者没有告诉我们什么？", @"url":@"http://tp4.sinaimg.cn/1888255567/180/1292411105/1", @"date":@"2014-08-08"}];
    }
    
    [self registerForKeyboardNotifications];
    self.commentTextField.delegate = self;
    self.commentBoxView.layer.zPosition = 2.0;
    
    [self setUpForDismissKeyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f",scrollView.contentOffset.y);
    if (scrollView.contentOffset.y < -50) {
        [self.commentVCDelegate scrollToTop];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.commentDataList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = [self.commentTableView dequeueReusableCellWithIdentifier:@"CommentCell" forIndexPath:indexPath];
    cell.data = self.commentDataList[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return [CommentCell heightForCommentCell:self.commentDataList[indexPath.row]];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)submitComment:(id)sender {
    
}

- (void)tapAnywhereToDismissKeyboard:(UITapGestureRecognizer *)sender
{
    [self.view endEditing:YES];
}

- (void)setUpForDismissKeyboard
{
    UITapGestureRecognizer *singleTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAnywhereToDismissKeyboard:)];
    
    NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
        [self.commentTableView addGestureRecognizer:singleTapGR];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) {
        [self.commentTableView removeGestureRecognizer:singleTapGR];
    }];
}

- (void)postComment
{
    NSLog(@"post comment.");
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    [self postComment];
    return YES;
}


- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShown:(NSNotification *)notification
{
    NSLog(@"keyboardWillShown");
    [self.view bringSubviewToFront:self.commentBoxView];
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat offsetY = self.commentBoxView.frame.origin.y - keyboardFrame.size.height;
    
    [UIView animateWithDuration:0.8 animations:^{
        CGRect rect = self.commentBoxView.frame;
        rect.origin.y = offsetY;
        self.commentBoxView.frame = rect;
    }];
}

- (void)keyboardWillBeHidden:(NSNotification *)notification
{
    NSLog(@"keyboardWillBeHidden");
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat offsetY = self.commentBoxView.frame.origin.y + keyboardFrame.size.height;
    
    [UIView animateWithDuration:0.8 animations:^{
        CGRect rect = self.commentBoxView.frame;
        rect.origin.y = offsetY;
        self.commentBoxView.frame = rect;
    }];
}




@end









